/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.NotifyLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.NotifyLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;


/**
 *
 *
 * @author lhd
 * @date 2020-05-12 08:21:24
 */
@RestController
@AllArgsConstructor
@RequestMapping("/p/myNotifyLog" )
@Api(tags = "消息接口")
public class NotifyLogController {

    private final NotifyLogService notifyLogService;


    /**
     * 查询未读消息数量
     */
    @GetMapping("/unReadCount" )
    @ApiOperation(value = "查询未读消息数量", notes = "查询未读消息数量")
    public ResponseEntity<Integer> getNotifyCount() {
        String userId = SecurityUtils.getUser().getUserId();
        return ResponseEntity.ok(notifyLogService.count(new LambdaQueryWrapper<NotifyLog>()
                .eq(NotifyLog::getRemindId,userId)
                .eq(NotifyLog::getStatus,0)
                .lt(NotifyLog::getSendType, 100)
                .eq(NotifyLog::getRemindType,1)));
    }


    /**
     * 查询未读消息列表
     */
    @GetMapping("/unReadCountList")
    @ApiOperation(value = "查询消息列表", notes = "查询消息列表")
    @ApiImplicitParam(name = "status", value = "状态 0未读 1已读", required = false, dataType = "Integer")
    public ResponseEntity<IPage<NotifyLog>> getUnReadCountList(PageParam<NotifyLog> page, Integer status) {
        String userId = SecurityUtils.getUser().getUserId();
        status = Objects.isNull(status)?0:status;
        IPage<NotifyLog> notifyReminds = notifyLogService.page(page,
                new LambdaQueryWrapper<NotifyLog>()
                        .eq(NotifyLog::getRemindId,userId)
                        .eq(NotifyLog::getStatus, status)
                        .lt(NotifyLog::getSendType, 100)
                        .eq(NotifyLog::getRemindType, 1)
                        .orderByDesc(NotifyLog::getCreateTime));
        List<NotifyLog> records = notifyReminds.getRecords();
        records.forEach(record -> record.setStatus(1));
        if(CollectionUtils.isNotEmpty(records)) {
            notifyLogService.updateBatchById(records);
        }
        return ResponseEntity.ok(notifyReminds);
    }



}
